/**
 * 实现弹出窗口幻灯效果
 */
function slider_factory( id ){
         //哪一个元素触发了 slider 
         //从 id 中分割出 product_class_name , 以及具体的文件夹名称
         var call_slider_el_id = id.split( '-' );
         //当前显示页面
         this.current_page_html;
	 this.product_class_name = call_slider_el_id[0];
	 this.sub_dir_name = call_slider_el_id[1];
         //读取默认页面
         this.async_get_page( '' , true );
}
//显示上一页
slider_factory.prototype.priv_page = function(){
         this.async_get_page( 'priv' );
};
//显示下一页
slider_factory.prototype.next_page = function(){
         this.async_get_page( 'next' );
};
//清除内容
slider_factory.prototype.clear = function(){
        $( '#base_index_detail_product_item_nav_slider_ul .base_index_detail_product_item_nav_slider_li' ).text( '' );
}
//显示商品列表
slider_factory.prototype.enable_product_list = function(){
///{{{
	$( '.base_index_detail_product_item_nav_slider_product_list_tr' ).each( function( index , el ){
		 var this_el = $( el );        
		 //获得 product_id 以及 class_id
		 var this_product_id = this_el.find( '.base_index_detail_product_item_nav_slider_product_id' ).text();
		 var this_class_id = this_el.find( '.base_index_detail_product_item_nav_slider_class_id' ).text();
		 this_el.bind({
			 'mouseover' : function(){
				 this_el.find('td').attr( 'style' , 'background:#f9f9f9'  );
			 },
			 'mouseout' : function(){
				 this_el.find('td').attr( 'style' , '' );
			 }
		 });
		 this_el.click( function(){
			 //现在需要判断用户是否已经登录系统先
			 //没有登录的话就会被导向到登录页面
			 $.ajax({
				 dataType: 'json',
				 type: "GET",
				 url: "/auth/is_login/",
				 success: function( data ){
					 var is_login = data.res;
					 if( is_login == false ){
						 window.location.href = '/auth/login/';
						 return;
					 }else{
						 window.location.href = '/deal/buy/product_id/' + this_product_id + 
								 '/class_id/' + this_class_id;
						 return;
					 }
				 },
				 error: function(){
					 alert( 'ooh!' );
				 }
			 });
		 });  
	 });//end each

	 //如果商品过多 需要分页来显示
	 var pager_links_el = $( '#pager_links' );
	 if( pager_links_el != '' ){
		 //为每一个 a 绑定事件
		 links_el = pager_links_el.find( 'a' );
		 links_el.each( function( index , el ){
			 var this_el = $( el );
			 var pager_no = this_el.attr( 'href' ).split( '=' )[1];
			 this_el.click( function(){
				 var product_class_name = $( '.product_class_name' ).text();
				 //获取相应的新的页面信息
				 $.ajax({
					 dataType : "html" ,
					 cache : false ,
					 type : "GET" ,
					 url : "/base/get_sub_page/" ,
					 data : "pager=" + pager_no +
						 "&&product_class_name=" + product_class_name +
						 "&&sub_dir_name=detail_category_list" ,
					 success: function( data ){
						//更新页面信息
						var product_list_li_el = 
							$( ".base_index_detail_product_item_nav_slider_li" );
						product_list_li_el.find( '#pager_links' ).remove();
						product_list_li_el.find( 'table' ).remove();
						$( '.product_class_name' ).remove();
						$( '.sub_dir_name' ).remove();
						product_list_li_el.append( data );
                                                slider_factory.prototype.auto_set_height();
					        slider_factory.prototype.enable_product_list();
					 } ,
					 error: function(){
					        alert( 'get product list error.' );
					 }	 	 
				 });
			 }); 
		 });
	 }
}///}}}
//设置弹出窗口的高度以及位置
slider_factory.prototype.auto_set_height = function(){
//{{{
         var slider_li_el = $( '.base_index_detail_product_item_nav_slider_li' );
         var slider_li_el_height = slider_li_el.height(); 
         $( '#base_index_detail_product_item_nav_slider_ul' ).css({"overflow-y":"auto"});    
         if( slider_li_el_height > 550 ){
                 slider_li_el_height = 550;
                 $( '#base_index_detail_product_item_nav_slider_ul' ).css({"overflow-y":"scroll","overflow-x":"hidden","height":"550"});
         }
         if( slider_li_el_height < 200 ){
                 slider_li_el_height = 200;
         }
         $( "#base_index_detail_product_item_nav_slider" ).animate({ "height":slider_li_el_height + 33 } , 0 );
}//}}}
//异步获取页面信息
slider_factory.prototype.async_get_page = function( command , reset ){
//{{{
	$.ajax({
		 type : "GET",
		 dataType : "html",
		 cache : false,
		 url : "/base/get_sub_page/",
		 data : "command=" + command + "&&product_class_name=" + this.product_class_name + '&&sub_dir_name=' + this.sub_dir_name + '&&reset=' + reset ,
		 error : function(){
			 throw new Error( "获取页面信息时出错" );
		 },
		 success : function( res ){
			 //隐藏原有的页面
                         var slider_li_el = $( '#base_index_detail_product_item_nav_slider_ul .base_index_detail_product_item_nav_slider_li' );
			 slider_li_el.hide();
			 slider_li_el.html( res );
			 slider_li_el.fadeIn( 'slow' );
                         //根据返回 html 的高度设置 弹出窗口的高度
                         slider_factory.prototype.auto_set_height();
                         var slider_li_height = slider_li_el.height();
                         //从 msg 中解析出 sub_dir_name, product_class_name
                         var sub_dir_name = $( '.sub_dir_name' ).text();
                         var product_class_name = $( '.product_class_name' ).text();
                         $( '.base_index_detail_product_item_nav_slider_sub_nav_a_text' ).attr( 'id' , product_class_name + '-text_describe-sub' );
                         $( '.base_index_detail_product_item_nav_slider_sub_nav_a_multimedia' ).attr( 'id' , product_class_name + '-multimedia_describe-sub' );
                         $( '.base_index_detail_product_item_nav_slider_sub_nav_a_feature' ).attr( 'id' , product_class_name + '-feature-sub' );
                         $( '.base_index_detail_product_item_nav_slider_sub_nav_a_detail' ).attr( 'id' , product_class_name + '-detail_category_list-sub' );
                         //清除之前所有的高亮显示
                         //@todo 似乎可以用闭包 优美的解决这个问题
                         $( '.base_index_detail_product_item_nav_slider_sub_nav_a' ).each( function( index , el ){
                                 var this_el = $( el );
                                 this_el.removeClass( 'selected' );
                         });
                         //高亮显示当前的子选项图标
                         var sub_nav_button = $( '#' + product_class_name + '-' + sub_dir_name + '-sub' );
                         sub_nav_button.mouseover();
                         sub_nav_button.addClass( 'selected' );
                         //如果是商品列表页面
                         if( sub_dir_name == 'detail_category_list' ){
				 slider_factory.prototype.enable_product_list();
                         }//end if 
		 }
	 }); 	
};//}}}

